<template>
  <div>
    <el-card class="!border-none mb-4" shadow="never">
      <el-form class="mb-[-16px]" :model="queryParams" inline>

        <!--        <el-form-item label="时间(年月)" prop="search_month">-->
        <!--          <el-select class="w-[280px]" v-model="queryParams.search_month" clearable placeholder="请选择时间(年月)">-->
        <!--            <el-option label="全部" value=""></el-option>-->
        <!--            <el-option-->
        <!--                v-for="(item, index) in searchLists.month_list"-->
        <!--                :key="index"-->
        <!--                :label="item.name"-->
        <!--                :value="item.id"-->
        <!--            />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->

        <el-form-item label="时间(年月)" prop="search_month">
          <el-date-picker
              value-format="YYYYMM"
              v-model="queryParams.search_month"
              type="month" placeholder="请选择时间(年月)"/>
        </el-form-item>

        <el-form-item label="院区/楼栋">
          <el-cascader
              :options="searchLists.cb_lists"
              clearable filterable
              v-model="queryParams.cb_ids"
              :props="props1"
          />
        </el-form-item>

        <el-form-item label="住户信息" prop="user_keyword">
          <el-input class="w-[280px]" v-model="queryParams.user_keyword" clearable placeholder="请输入住户名/手机号"/>
        </el-form-item>

        <el-form-item label="房间信息" prop="coefficient">
          <el-input
              class="w-[280px]"
              v-model="queryParams.room_keyword"
              clearable
              placeholder="请输入房间名/地址"
          />
        </el-form-item>

        <el-form-item label="房间类型" prop="room_type">
          <el-select class="w-[280px]" v-model="queryParams.room_type" clearable placeholder="请选择房间类型">
            <el-option label="全部" value=""></el-option>
            <el-option v-for="(item, index) in searchLists.type_list" :key="index" :label="item" :value="index"/>
          </el-select>
        </el-form-item>

        <!--        <el-form-item label="记录时间" prop="log_time">-->
        <!--          <daterange-picker-->
        <!--              v-model:startTime="queryParams.start_time"-->
        <!--              v-model:endTime="queryParams.end_time"-->
        <!--          />-->
        <!--        </el-form-item>-->

        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
          <!--          <el-button type="warning" @click="printBox = !printBox">打印/导出</el-button>-->
          <el-button type="warning" @click="handlePrint">打印</el-button>
          <export-data
              class="ml-2.5"
              button-text="导出(月份)"
              :fetch-fun="apiRoomHospitalExportLists"
              :params="queryParams"
              :page-size="pager.size"/>
          <export-data
              class="ml-2.5"
              button-text="导出(季度)"
              :fetch-fun="apiRoomHospitalExportSeason"
              :params="queryParams"
              :page-size="pager.size"/>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card v-show="printBox" class="mb-4">

    </el-card>

    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <!--      <el-button v-perms="['room.room_hospital_device_log/add']" type="primary" @click="handleAdd">-->
      <!--        <template #icon>-->
      <!--          <icon name="el-icon-Plus"/>-->
      <!--        </template>-->
      <!--        新增-->
      <!--      </el-button>-->
      <!--      <el-button v-perms="['room.room_hospital_device_log/delete']" :disabled="!selectData.length" @click="handleDelete(selectData)">-->
      <!--        删除-->
      <!--      </el-button>-->
      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">
          <!--          <el-table-column type="selection" width="55"/>-->
          <el-table-column label="序号" prop="sort" width="90" show-overflow-tooltip/>

          <el-table-column label="房间" prop="rid" width="200">
            <template #default="{ row }">
              {{ row.name }} <br/>
              <el-text v-if="row.campus">{{ row.campus.name }}-</el-text>
              <el-text v-if="row.building">-{{ row.building.name }}</el-text>
              <el-text v-if="row.room_department"><br/>{{ row.room_department }}</el-text>
            </template>
          </el-table-column>

          <el-table-column label="住户" prop="rid" width="150">
            <template #default="{ row }">
              {{ row.user_name }} <br/>
              {{ row.user_mobile }}
            </template>
          </el-table-column>

          <!--          <el-table-column label="设备" prop="device_id">-->
          <!--            <template #default="{ row }">-->
          <!--&lt;!&ndash;              <view v-if="row.device">&ndash;&gt;-->
          <!--                {{ row.device_name }} <br/>-->
          <!--                {{ row.device_no }} <br/>-->
          <!--&lt;!&ndash;                {{ row.device_type }}&ndash;&gt;-->
          <!--&lt;!&ndash;              </view>&ndash;&gt;-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <!--          <el-table-column label="设备系数" prop="coefficient" show-overflow-tooltip/>-->
          <!--          <el-table-column label="水电价" prop="price" show-overflow-tooltip/>-->

          <el-table-column label="总水用量" prop="room_s_use_total" show-overflow-tooltip/>
          <el-table-column label="均水价" prop="room_s_price" show-overflow-tooltip/>
          <el-table-column label="总水费用" prop="room_s_total_fee" show-overflow-tooltip/>

          <el-table-column label="总电用量" prop="room_d_use_total" show-overflow-tooltip/>
          <el-table-column label="均电价" prop="room_d_price" show-overflow-tooltip/>
          <el-table-column label="总电费用" prop="room_d_total_fee" show-overflow-tooltip/>

          <el-table-column label="电梯费" prop="room_lift_fee" show-overflow-tooltip/>
          <el-table-column label="二次压力费" prop="room_second_pressure_fee" show-overflow-tooltip/>

          <el-table-column label="总费用" prop="room_all_total_fee" show-overflow-tooltip/>
          <el-table-column label="记录时间" prop="log_date" show-overflow-tooltip/>

          <!--          <el-table-column label="操作" width="120" fixed="right">-->
          <!--            <template #default="{ row }">-->
          <!--              <el-button-->
          <!--                  v-perms="['room.room_hospital_device_log/edit']"-->
          <!--                  type="primary"-->
          <!--                  link-->
          <!--                  @click="handleEdit(row)"-->
          <!--              >-->
          <!--                编辑-->
          <!--              </el-button>-->
          <!--              <el-button-->
          <!--                  v-perms="['room.room_hospital_device_log/delete']"-->
          <!--                  type="danger"-->
          <!--                  link-->
          <!--                  @click="handleDelete(row.id)"-->
          <!--              >-->
          <!--                删除-->
          <!--              </el-button>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>

    <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false"/>

    <print-popup v-if="showPrint" ref="printRef" @close="showPrint = false"
                 :print-keyword="printLists.pkl_keyword"
                 :print-lists="printLists.pkl_lists"/>

  </div>
</template>

<script lang="ts" setup name="roomHospitalDeviceLogLists">
import {usePaging} from '@/hooks/usePaging'
import {useDictData} from '@/hooks/useDictOptions'
import {
  apiRoomHospitalDeviceLogLists,
  apiRoomHospitalDeviceLogDelete,
  apiRoomHospitalPrintListsDeviceLog,
  apiRoomHospitalExportLists,
  apiRoomHospitalExportSeason,
} from '@/api/room/room_hospital_device_log'
import {timeFormat} from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {reactive} from "vue";
import {hospitalSearchLists} from "@/api/room/room_hospital";

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)
const props1 = {
  checkStrictly: true
}

const printBox = ref(false);

import PrintPopup from '@/views/system/print_page.vue'

const printRef = shallowRef<InstanceType<typeof PrintPopup>>()
const showPrint = ref(false)
const printLists = reactive({
  pkl_keyword: [],
  pkl_lists: [],
})
const handlePrint = async () => {
  const print = await apiRoomHospitalPrintListsDeviceLog(queryParams)
  printLists.pkl_keyword = print.keyword
  printLists.pkl_lists = print.lists
  showPrint.value = true
  await nextTick()
  printRef.value?.open()
}


// 查询条件
const queryParams = reactive({
  cid: '',
  rid: '',
  device_id: '',
  coefficient: '',
  price: '',
  base_number: '',
  reading: '',
  use_total: '',
  lift_fee: '',
  second_pressure_fee: '',
  total_fee: '',
  log_time: '',
  start_time: '',
  end_time: '',
  cb_ids: '',
  room_keyword: '',
  room_type: '',
  device_keyword: '',
  search_month: '',
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({id}) => id)
}

// 获取字典数据
const {dictData} = useDictData('show_status')

const searchLists = reactive({
  cb_lists: [],
  month_list: [],
  type_list: [],
})
const getSearchLists = async () => {
  const lists = await hospitalSearchLists([])
  searchLists.cb_lists = lists.cb_lists
  searchLists.month_list = lists.month_list
  searchLists.type_list = lists.type_list
  return lists;
}
getSearchLists()

// 分页相关
const {pager, getLists, resetParams, resetPage} = usePaging({
  fetchFun: apiRoomHospitalDeviceLogLists,
  params: queryParams
})

// 添加
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm('确定要删除？')
  await apiRoomHospitalDeviceLogDelete({id})
  getLists()
}

getLists()
</script>

